package com.wl.mvi.web

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.webkit.JavascriptInterface
import com.wl.lib.mvi.BaseBindingActivity
import com.wl.mvi.web.databinding.ActivityWebViewBinding

/**
 *
 * Created by wangliang on 2024/11/25
 */
class CommonWebActivity : BaseBindingActivity<ActivityWebViewBinding>() {

    companion object {
        private const val JS_BRIDGE_NAME = "jsbridge"

        private const val URL = "url"
        fun launchActivity(context: Context, url: String) {
            val intent = Intent(context, CommonWebActivity::class.java)
            intent.putExtra(URL, url)
            context.startActivity(intent)
        }
    }

    private var jsDisable = false
    private val jsBridge by lazy { JsBridge(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val url = intent?.getStringExtra(URL) ?: ""
        if (TextUtils.isEmpty(url)) {
            Log.e("CommonWebActivity", "load url is null")
            finish()
            return
        }

        binding.ivBack.setOnClickListener { finish() }

        binding.tvTitle.setOnClickListener {
            binding.webView.evaluateJavascript("(function() { return window.nativeCallTestMethod!=undefined ? nativeCallTestMethod('test', 3): false; })();"){ res ->
                Log.d("WLTest", "result: $res")
            }
        }

        initWebView()
//        val path =
//            FileUtils.getExternalFileWebRootPath(this) + "dev-error_2aab66bf-0975-4cec-8cd8-92651381454b" + "/dist/index.html"
//        Log.d("WLTest", "path: $path")
        binding.webView.loadUrl(url)
    }

    @SuppressLint("JavascriptInterface")
    private fun initWebView() {
        if (!jsDisable) {
            binding.webView.addJavascriptInterface(jsBridge, JS_BRIDGE_NAME)
        }

        binding.webView.navigator = object : Navigator {
            override fun startActivityForResult(intent: Intent, requestCode: Int) {
                this@CommonWebActivity.startActivityForResult(intent, requestCode)
            }
        }

        // 如果页面中链接，如果希望点击链接继续在当前browser中响应，
        // 而不是新开Android的系统browser中响应该链接，必须覆盖webview的WebViewClient对象
        binding.webView.setWebViewClient(CommonWebViewClient(binding.progressBar, binding.tvTitle))
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        binding.webView.onActivityResult(requestCode, resultCode, data)
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun onDestroy() {
        binding.webView.destroy()
        super.onDestroy()
    }

}

private class JsBridge(val context: Context) {

    private val handler by lazy { Handler(Looper.getMainLooper()) }

    @JavascriptInterface
    fun openWebView(url: String?) {
        url ?: return
        Log.d("WLTest", "openWebView url: $url")
        handler.post { CommonWebActivity.launchActivity(context, url) }
    }
}

